﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Windows.Forms;
using RibbonEditor.AppCode;

namespace RibbonEditor.LocLabels
{
    public class Title: IXmlBuildable
    {
        XmlNode titleXmlNode;

        public string LanguageCode { get; set; }
        public string Description { get; set; }

        public Title()
        { }

        public Title(XmlNode node)
        {
            try
            {
                this.titleXmlNode = node;

                this.LanguageCode = (string)XmlHelper.ExtractXmlAttributeToProperty2(node, "languagecode", XmlHelper.PropertyType.stringType);
                this.Description = (string)XmlHelper.ExtractXmlAttributeToProperty2(node, "description", XmlHelper.PropertyType.stringType);
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        internal void AddTitleNode(XmlNode LocLabelsXmlNode)
        {
            try
            {
                XmlNode titleNode = LocLabelsXmlNode.OwnerDocument.CreateElement("Title");

                XmlAttribute languageCodeAttribute = LocLabelsXmlNode.OwnerDocument.CreateAttribute("languagecode");
                languageCodeAttribute.Value = this.LanguageCode;
                titleNode.Attributes.Append(languageCodeAttribute);

                XmlAttribute descriptionAttribute = LocLabelsXmlNode.OwnerDocument.CreateAttribute("description");
                descriptionAttribute.Value = this.Description;
                titleNode.Attributes.Append(descriptionAttribute);

                LocLabelsXmlNode.AppendChild(titleNode);
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        internal void BuildTreeViewElement(TreeNode parentNode)
        {
            try
            {
                TreeNode titleNode = new TreeNode("Title");
                titleNode.Tag = this;

                parentNode.Nodes.Add(titleNode);
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        #region IXmlBuildable Members

        public void AddNode(XmlNode parentNode)
        {
            this.AddTitleNode(parentNode);
        }

        #endregion
    }
}
